package com.web;

import com.dao.impl.BookDaoImpl;
import com.javabean.Cart;
import com.javabean.Order;
import com.javabean.OrderItem;
import com.javabean.User;
import com.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    OrderServiceImpl a = new OrderServiceImpl();

    //生成订单
    public void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //先获取cart购物车对象
        //获取session域中的购物车对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        //获取userId
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            return;
        }
        Integer id = user.getId();
        String orderid = a.createOrder(cart, id);
        request.getSession().setAttribute("orderid", orderid);
        response.sendRedirect(request.getContextPath() + "/pages/cart/checkout.jsp");
    }


    public void showMyOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        List<Order> orders = a.showMyOrders(i);
        request.getSession().setAttribute("order", orders);
        request.getRequestDispatcher("/pages/order/order.jsp").forward(request, response);
    }


    public void showOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderid = request.getParameter("orderid");
        List<OrderItem> orderItems = a.showOrderDetail(orderid);
        request.getSession().setAttribute("orderItems", orderItems);
        request.getRequestDispatcher("/pages/order/orderdetail.jsp").forward(request, response);
    }


    public void showAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Order> allorders = a.showAllOrders();
        request.getSession().setAttribute("allOrders", allorders);
        request.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(request, response);
    }

    public void sendOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderid = request.getParameter("orderid");
        a.sendOrder(orderid);
        //重定向会原来商品所在的地址页面
        response.sendRedirect(request.getHeader("Referer"));

    }

    public void receiveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderid = request.getParameter("orderid");
        a.receiveOrder(orderid);
        //重定向会原来商品所在的地址页面
        response.sendRedirect(request.getHeader("Referer"));
    }
}
